package com.ruoyi.web.controller.manage;

import java.util.List;

import com.ruoyi.manage.domain.SuitManagement;
import com.ruoyi.manage.mapper.SuitManagementMapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.manage.domain.UserOrder;
import com.ruoyi.manage.service.IUserOrderService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 用户订单Controller
 * 
 * @author ruoyi
 * @date 2021-03-08
 */
@RestController
@RequestMapping("/manage/order")
public class UserOrderController extends BaseController
{
    @Autowired
    private IUserOrderService userOrderService;
    @Autowired
    private SuitManagementMapper suitManagementMapper;

    /**
     * 查询用户订单列表
     */
    @PreAuthorize("@ss.hasPermi('manage:order:list')")
    @GetMapping("/list")
    public TableDataInfo list(UserOrder userOrder)
    {
        startPage();
        List<UserOrder> list = userOrderService.selectUserOrderList(userOrder);
        for (UserOrder order : list) {
            Long suitManageId = order.getSuitManageId();
            //根据婚纱套系id查询
            SuitManagement suitManagement = suitManagementMapper.selectSuitManagementById(suitManageId);
            order.setSuitManageName(suitManagement.getName());

        }
        return getDataTable(list);
    }

    /**
     * 导出用户订单列表
     */
    @PreAuthorize("@ss.hasPermi('manage:order:export')")
    @Log(title = "用户订单", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(UserOrder userOrder)
    {
        List<UserOrder> list = userOrderService.selectUserOrderList(userOrder);
        ExcelUtil<UserOrder> util = new ExcelUtil<UserOrder>(UserOrder.class);
        return util.exportExcel(list, "order");
    }

    /**
     * 获取用户订单详细信息
     */
    @PreAuthorize("@ss.hasPermi('manage:order:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(userOrderService.selectUserOrderById(id));
    }

    /**
     * 新增用户订单
     */
    @PreAuthorize("@ss.hasPermi('manage:order:add')")
    @Log(title = "用户订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserOrder userOrder)
    {
        return toAjax(userOrderService.insertUserOrder(userOrder));
    }

    /**
     * 修改用户订单
     */
    @PreAuthorize("@ss.hasPermi('manage:order:edit')")
    @Log(title = "用户订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserOrder userOrder)
    {
        return toAjax(userOrderService.updateUserOrder(userOrder));
    }

    /**
     * 删除用户订单
     */
    @PreAuthorize("@ss.hasPermi('manage:order:remove')")
    @Log(title = "用户订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(userOrderService.deleteUserOrderByIds(ids));
    }
}
